<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */
class SomeClientModel extends Crud
{
    public function __construct()
    {
        parent::__construct();
    }
 
    public function getClientData($id)
    {
        return $this->get_one('tb_client','id',$id);
    }
    
    public function getOrderData($id)
    {
        $r = $this->get_one('tb_order', 'id_client', $id);
        foreach($r as $key =>$value)
        {
           if($value['status']=='Отменено')
           {
                unset($r[$key]);
           }
           else
           {
                $person =  $this->get_one('tb_person', 'id', $value['id_person']);
                if(empty($person))
                {
                    $r[$key]['person'] = 'Пользователь удалён';
                }
                else
                {
                    $r[$key]['person'] = $person[0]['name']." ".$person[0]['first_name']." ".$person[0]['second_name'];
                }
                
                $r[$key]['id_product'] = $this->parse_product($value['id_product']);
                $r[$key]['count']    = $this->parse_product($r[$key]['count']);
                $r[$key]['priceall'] = 0;
                foreach($r[$key]['id_product'] as $k => $v){
                    $p = $this->get_one('tb_product', 'id', $v);
                    $r[$key]['product'][] = $p[0]['name'];
                    $r[$key]['price'][] = $p[0]['price'];
                    $r[$key]['priceall'] += (float)@$p[0]['price']*$r[$key]['count'][0];
                    $p = $this->get_one('tb_fabricator', 'id', $p[0]['id_fabricator']);
                    $r[$key]['fabricator'][] =  $p[0]['name'];
                    
                }
           }
        }
        return $r;
    }
    
        
/*===============================Вывод всех городов===========================*/   
    public function City($region)
    {
        
        $rez = array();
        if(is_string($region))
        {
            $r = $this->get_one('region','name', $region);
            $rez = $this->get_one('city','region_id', $r[0]['region_id']);
        }
        else
        {
            $rez = $this->get_one('city','region_id', $region['id_region']);
        }

        $list_city = array();
        $i =0;
        foreach ($rez as $key => $val)
        {
            $k = $rez[$key]['city_id'];
            $list_city[$i]['city'] = $rez[$key]['name'];
            $list_city[$i]['city_id'] = $k;
            $i++;
        }
        return $list_city;
    }
  
/*===============================Список заказов===============================*/   
    public function getReport($id)
    {
       $r['count_order'] = count($this->getOrderData($id));
       $q =  $this->getOrderData($id);   
       
       $r['total_cost'] = 0;
       foreach($q as $key => $value)
       {
           foreach($value['id_product'] as $k=>$v)
           {
              $temp[] = $this->get_one('tb_product', 'id', $v);
           }
           foreach( $temp as $k=>$v)
           {
                $r['total_cost']+= (float)$temp[$k][0]['price'];
           }
          
       }
       return $r;
    }
    
/*===============================Создание excel===============================*/
    public function clientToExcel($id)
    {
        $client_data = $this->getClientData($id);
        $report_data = $this->getReport($id);
        
        $objPHPExcel = $this->ci_phpecxel;
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        
        $styleArray = array(
            'font' => array(
                'bold' => true,
                'size' => '16'
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
            ),
        );

        $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->applyFromArray($styleArray);
        
        $objPHPExcel->getActiveSheet()->setCellValue('A1','Код клиента');
        $objPHPExcel->getActiveSheet()->setCellValue('A2', $client_data[0]['id']); 
        
        $objPHPExcel->getActiveSheet()->setCellValue('B1','Фамилия');
        $objPHPExcel->getActiveSheet()->setCellValue('B2', $client_data[0]['name']); 
        
        $objPHPExcel->getActiveSheet()->setCellValue('C1','Имя');
        $objPHPExcel->getActiveSheet()->setCellValue('C2', $client_data[0]['first_name']); 
        
        $objPHPExcel->getActiveSheet()->setCellValue('D1','Отчество');
        $objPHPExcel->getActiveSheet()->setCellValue('D2', $client_data[0]['second_name']);  
        
        $objPHPExcel->getActiveSheet()->setCellValue('E1','Телефон');
        $objPHPExcel->getActiveSheet()->setCellValue('E2', $client_data[0]['phone']);  
        
        $objPHPExcel->getActiveSheet()->setCellValue('F1','Всего заказов');
        $objPHPExcel->getActiveSheet()->setCellValue('F2', $report_data['total_cost']);   
        
        $objPHPExcel->getActiveSheet()->setTitle('Клиент');
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
         $path ='doc/client/';
        $filename = 'clientdata-'.$id.'.xlsx';
        $objWriter->save($path.$filename);
        
        return "<a href='/$path$filename'>$filename</a>";
    }
    
}